<?php

/*
# Application Scope Variables Include File
# Developed by
# 	Phillip Harrington(phil@philsown.com)
# 	David Huyck(b@bombusbee.com)
*/

if(!class_exists('AppScope')){

	class AppScope{

		var $appvars=array();
		var $compare_vars=array();
		var $file;
		var $scope_name;
		var $changes_since_read=0;
		var $fp;
		var $content;

		function AppScope($file='_APPLICATION.php',$scope_name='_APPLICATION',$appvars=array()){
			$this->file=$file;
			$this->scope_name=$scope_name;
			if(count($appvars)>0){
				$this->Read($appvars);
			}
		}

		function em($msg) {
			if(!empty($msg)){
				echo "<p><b>AppVars Error: </b> " . $msg . "</p>\n";
			}
		}

		function Read($appvars){
			$this->appvars=$appvars;
		}

		function Dump(){
			return $this->appvars;
		}

		function __set($name, $value) {
			$this->Set($value, $name);
		}

		function __get($name) {
			return $this->appvars[$name];
		}

		function Set($value,$name=''){
			eval("global \${$this->scope_name};");
			if(!is_array($value)){
				$vtype=gettype($value);
				eval("\$compare=\$this->appvars['$name'];");
				eval("\$ctype=gettype(\$this->appvars['$name']);");
				if(($compare!=$value)||($ctype!=$vtype)){
					$this->changes_since_read=1;
					switch(strtolower(gettype($vtype))){
						case 'boolean':
							if($value===TRUE){
								eval("\$this->appvars['$name']=TRUE;");
								eval("\${$this->scope_name}['$name']=TRUE;");
							}elseif($value===FALSE){
								eval("\$this->appvars['$name']=FALSE;");
								eval("\${$this->scope_name}['$name']=FALSE;");
							}
							break;
						case 'integer':
						case 'double':
						case 'string':
							eval("\$this->appvars['$name']=\$value;");
							eval("\${$this->scope_name}['$name']=\$value;");
							break;
						case 'array':
						case 'object':
						case 'resource':
						case 'null':
						case 'user function':
						case 'unknown type':
							break;
						default:
							break;
					}
				}
			}else{
				foreach($value as $key=>$val){
					if(!empty($name)){
						$this->Set($val,"$name']['$key");
					}else{
						$this->Set($val,$key);
					}
				}
			}
		}

		function uSet($name){
			if(!empty($this->appvars[$name])){
				eval("unset(\$this->appvars[\$name]);");
				eval("global \${$this->scope_name};");
				eval("unset(\${$this->scope_name}[\$name]);");
				$this->changes_since_read=1;
			}
		}

		function Write(){
			eval("global \${$this->scope_name};");
			eval("\$this->compare_vars=\${$this->scope_name};");
			$this->Set($this->compare_vars);
			if($this->changes_since_read==1){
				$this->content="<?php\n";
				$this->Compile($this->scope_name,$this->appvars);
				$this->content.="\n?>";
				$this->content=trim($this->content);
				if($this->fp=fopen($this->file,'w')){
					fwrite($this->fp,$this->content,strlen($this->content));
					fclose($this->fp);
				}else{
					$this->em("I can't write to the " . $this->file . " file.
					If you simply chmod 777 the parsed directory,
					reload this page ONCE, then chmod the directory back
					to the previous permissions, then this will work. Any
					decent FTP client will allow you to do this. The file
					that is generated will be owned by Apache and will be
					able to be written to as often as needed.");
				}
			}
		}

		function Compile($name,$ary){
			foreach($ary as $key=>$value){
				if(!is_array($value)){
					$line='$'.$name."['".$key."']=";
					switch (strtolower(gettype($value))) {
						case 'boolean':
							if ($value==TRUE) {
								$line.='TRUE';
							} else if ($value==FALSE) {
								$line.='FALSE';
							}
							break;
						case 'integer':
						case 'double':
							$line.=$value;
							break;
						case 'string':
							$value=str_replace("\\","\\\\",$value);
							$value=str_replace("'","\'",$value);
							$line.="'" . $value . "'";
							break;
						case 'array':
						case 'object':
						case 'resource':
							break;
						case 'null':
							$line.='null';
							break;
						case 'user function':
						case 'unknown type':
							break;
						default:
							break;
					}
					$line.=";\n";
					$this->content.=$line;
				}else{
					if(count($value)>0){
						$line='$'.$name."['".$key."']=array();\n";
						$this->content.=$line;
						$this->Compile($name."['".$key."']",$value);
					}
				}
			}
		}

	}

}
?>